﻿using System;
using System.Collections.Generic;
using System.Text;
using Comm.PublicClass;


namespace WsProtocol.IEC62056.DataTypeClass
{    
    // hxh 2018-5 62056复杂类型处理
    public class PIID {
        public static string GetFormatValue(string obis)
        {
            string FrameValue = "81";

            
            return FrameValue;

        }

        public static string AnalizeData(ref string frameData, ref string errorinfo)
        {
            
            if (string.IsNullOrEmpty(frameData))
            {
                return frameData;
            }
            try
            {
                string str = frameData.Substring(0, 2);
                frameData = frameData.Substring(2);
                return str;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }
    }
    public class OBIS 
    {
        public static string GetFormatValue(string obis, ref string errorinfo)
        {
            string FrameValue = string.Empty;
            obis = obis.Replace(':', '.').Replace('-', '.').Replace(',', '.').Replace(" ", "");
            string[] oblist = obis.Split('.');
            for (int i = 0; i < oblist.Length; i++)
            {
                FrameValue += MyConvert.DecToHex(oblist[i], 2);
            }
            return FrameValue.PadLeft(12, '0');

        }

        public static string GetFormatValue(ElementData eNode, ref string errorinfo)
        {            
            return GetFormatValue(eNode.SetValue, ref errorinfo);

        }
        public static int AnalizeData(ref string frameData, ElementData eNode, ref string errorinfo)
        {
                    
            try
            {
                eNode.ReadValue = string.Empty;
                eNode.FrameValue = eNode.ReadValue;
                for (int i = 0; i < 6; i++) {
                    string stmp = frameData.Substring(0, 2);
                    eNode.ReadValue += Convert.ToInt32(stmp,16) + ".";
                    frameData = frameData.Substring(2);
                }
                eNode.ReadValue = eNode.ReadValue.Substring(0, eNode.ReadValue.Length - 1);
                return 0;
            }
            catch (Exception ex)
            {
                eNode.FrameValue = eNode.ReadValue = frameData;   
                errorinfo = "非法的OBIS数据，解析终止";// ex.ToString();
                return -1;
            }

        }

        public static string AnalizeData(string obis, ref string errorinfo)
        {
            string str = string.Empty;
            if (string.IsNullOrEmpty(obis))
            {
                return obis;
            }
            try
            {               
                for (int i = 0; i < 6; i++)
                {
                    string stmp = obis.Substring(0, 2);
                    str += Convert.ToInt32(stmp, 16) + ".";
                    obis = obis.Substring(2);
                }
                str = str.Substring(0, str.Length - 1);
                return str;
            }
            catch (Exception ex)
            {
                errorinfo = "非法的OBIS数据，解析终止";
                return str;
            }
        }
    }
    /// <summary>
    /// 对象属性描述符OAD（Object Attribute Descriptor）
    /// </summary>
    public class CAD 
    {
            
        public static string GetFormatValue(CosemObject mOI,ref string errorinfo)
        {
            try
            {
                string obis = mOI.OBIS;
                string classid = mOI.classid;
                string Seq = mOI.AttrSeq;
                return MyConvert.DecToHex(classid,4) + OBIS.GetFormatValue(obis,ref errorinfo) + MyConvert.DecToHex(Seq, 2);
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static string GetFormatValue(ElementData eNode, ref string errorinfo)
        {
            return eNode.SetValue.PadLeft(18, '0');

        }

        public static string GetFormatValue(string classid, string obis, string Seq, ref string errorinfo)
        {
            return classid.PadLeft(4, '0') + OBIS.GetFormatValue(obis,ref errorinfo) + MyConvert.DecToHex(Seq, 2);

        }

        public static int AnalizeData(ref string frameData, CosemObject mOI, ref string errorinfo)
        {
            if (frameData.Length < 8)
            {
                errorinfo = "帧错误:未回OAD";
                return -1;
            }
            try
            {
                mOI.classid = frameData.Substring(0, 2 * 2);
                mOI.OBIS = OBIS.AnalizeData(frameData.Substring(4, 12),ref errorinfo);
                mOI.AttrSeq = frameData.Substring(16, 2);
                frameData = frameData.Substring(9 * 2);
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }
        public static int AnalizeData(ref string frameData, ElementData eNode, ref string errorinfo)
        {
            if (string.IsNullOrEmpty(frameData))
            {
                return -1;
            }
            try
            {                
                eNode.ReadValue = frameData.Substring(0, 9 * 2);
                eNode.FrameValue = eNode.ReadValue;
                //eNode.OAD = eNode.ReadValue;
                frameData = frameData.Substring(9 * 2);
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }
        public static string AnalizeData(ref string frameData, ref string errorinfo)
        {
            string RecValue = string.Empty;
            errorinfo = string.Empty;
            if (string.IsNullOrEmpty(frameData))
            {
                errorinfo = "解析数据为空";
            }
            try
            {
                RecValue = frameData.Substring(0, 9 * 2);
                frameData = frameData.Substring(9 * 2);

            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
            }
            return RecValue;
        }

    }

    public class SAD   //Selective-Access-Descriptor
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            if (eleNode == null || eleNode.eleNodeList == null || eleNode.eleNodeList.Count == 0) return string.Empty;

            string FrameValue = MyConvert.DecToHex(eleNode.SetValue, 2);

            FrameValue += Data.GetFormatValue(eleNode, ref errorinfo);
           
            return FrameValue;
        }
        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo) {
            int iresult = 0;
            return iresult;
        }
    
    }

    public class ADWithSel  //Cosem-Attribute-Descriptor-With-Selection 
    {
        public static string GetFormatValue(CosemObject mOI,ref string errorinfo)
        {
            string oad = CAD.GetFormatValue(mOI,ref errorinfo);
            string SelA = SAD.GetFormatValue(mOI.ParEleNode, ref errorinfo);
            if (!string.IsNullOrEmpty(SelA))
                return oad + "01" + SelA;
            else
                return oad + "00";
        }

        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            int iresult = 0;
            return iresult;
        }

    }
    
    
    //SEQUENCE
    //{
    //last-block      BOOLEAN,
    //block-number   Unsigned32,
    //raw-data       OCTETSTRING
    //}
    public class DataBlockSA  //DataBlock-SA        
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            try
            {
                string islast = Bool.GetFormatValue(eleNode.eleNodeList[0], ref errorinfo);
                string bNum = doublelongunsigned.GetFormatValue(eleNode.eleNodeList[1], ref errorinfo);
                string sData = Data.GetFormatValue(eleNode.eleNodeList[2], ref errorinfo);

                return islast + bNum + sData;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static string GetFormatValue(string FrameData,ref int DataLen, double frameseq, ref string errorinfo)
        {
            string islast = "01";
            try
            {
                ElementData feleNode = new ElementData();
                feleNode.SetValue = frameseq.ToString();
                string bNum = doublelongunsigned.GetFormatValue(feleNode, ref errorinfo);
                string slen = BERiLen.GetFormatValue(DataLen);
                DataLen = DataLen * 2 - slen.Length - bNum.Length;// - 2
                
                string mblockData = FrameData;               
                if (FrameData.Length > DataLen)
                {
                    mblockData = FrameData.Substring(0, DataLen);
                    islast = "00";
                }
                DataLen = mblockData.Length;
                slen = BERiLen.GetFormatValue(DataLen / 2);
                return islast + bNum + slen + mblockData;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static int AnalizeData(ref string frameData, ElementData eleNode, ref long frameseq, ref bool finished,ref string RawData, ref string errorinfo)
        {
            int iresult = 0;
            try
            {
                finished = Bool.AnalizeData(ref frameData, ref errorinfo);
                ElementData feleNode = new ElementData();
                iresult = doublelongunsigned.AnalizeData(ref frameData, feleNode, ref errorinfo);
                frameseq = Convert.ToInt64(feleNode.ReadValue);

                RawData = octString.AnalizeDataSpecial(ref frameData, ref errorinfo);
               // eleNode.FrameValue = framestr;

               // iresult = Data.AnalizeData(ref frameData, eleNode, ref errorinfo); //要不要解析再定
            }
            catch (Exception ex) {
                errorinfo = ex.ToString();
                iresult = -1;
            }
            return iresult;
        }

    }

    public class IdentifiedKey {
        public static string GetFormatValue(string keyID, ref string errorinfo)
        {
            try
            {
                return keyID.Length < 2 ? "0" + keyID : keyID.Substring(0, 2);
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static string GetFormatValue(KeyId keyID, ref string errorinfo)
        {
            try
            {
                return MyConvert.DecToHex(((int)keyID).ToString(), 2);
            }
            catch (Exception ex) {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static KeyId AnalizeData(ref string frameData, ref string errorinfo)
        {
            try
            {
                string framestr = frameData.Substring(0, 2);
                frameData = frameData.Substring(2);
                return (KeyId)int.Parse(framestr);
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return (KeyId)3;
            }
            
        }
    }


    public class WrappedKey {
        public static string GetFormatValue(string keyID,string keyCipheredData, ref string errorinfo)
        {
            try
            {
                string keyid = keyID.Length < 2 ? "0" + keyID : keyID.Substring(0, 2);
                string KeyData = octString.GetFormatValue(keyCipheredData, ref errorinfo);
                return keyid + KeyData;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static string GetFormatValue(KeyId keyID, string keyCipheredData, ref string errorinfo)
        {
            try
            {
                string keyid = MyConvert.DecToHex(((int)keyID).ToString(), 2);
                string KeyData = octString.GetFormatValue(keyCipheredData, ref errorinfo);
                return keyid + KeyData;
            }
            catch (Exception ex) {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static int AnalizeData(ref string frameData,ref string KeyID,ref string KeyData, ref string errorinfo)
        {
            try
            {                
                KeyID = frameData.Substring(0, 2);
                frameData = frameData.Substring(2);
                KeyData = octString.AnalizeDataSpecial(ref frameData, ref errorinfo);
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }
            
        }
    }

    public class AgreedKey
    {
        public static string GetFormatValue(string keyParameters, string keyCipheredData, ref string errorinfo)
        {
            try
            {
                string keyP = octString.GetFormatValue(keyParameters, ref errorinfo);
                string KeyData = octString.GetFormatValue(keyCipheredData, ref errorinfo);
                return keyP + KeyData;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }


        public static int AnalizeData(ref string frameData, ref string keyParameters, ref string KeyData, ref string errorinfo)
        {
            try
            {
                keyParameters = octString.AnalizeDataSpecial(ref frameData, ref errorinfo);               
                KeyData = octString.AnalizeDataSpecial(ref frameData, ref errorinfo);
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }
    }

    public class KeyInfo
    {
        public static string GetFormatValue(string choice,string keyParameters, string keyCipheredData, ref string errorinfo)
        {
            try
            {
                string formatvalue = string.Empty;
                switch (int.Parse(choice)) {
                    case 0:
                        formatvalue = IdentifiedKey.GetFormatValue(keyParameters, ref errorinfo);
                        break;
                    case 1:
                        formatvalue = WrappedKey.GetFormatValue(keyParameters, keyCipheredData, ref errorinfo);
                        break;
                    case 2:
                        formatvalue = AgreedKey.GetFormatValue(keyParameters, keyCipheredData, ref errorinfo);
                        break;
                }
                return choice.PadLeft(2, '0') + formatvalue;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static string GetFormatValue(ElementData eleNode, ref string errorinfo)
        {
            try
            {
                string formatvalue = string.Empty;
                string choice = eleNode.SetValue;
                switch (int.Parse(choice))
                {
                    case 0:
                        formatvalue = IdentifiedKey.GetFormatValue(eleNode.eleNodeList[0].SetValue, ref errorinfo);
                        break;
                    case 1:
                        formatvalue = WrappedKey.GetFormatValue(eleNode.eleNodeList[0].SetValue, eleNode.eleNodeList[1].SetValue, ref errorinfo);
                        break;
                    case 2:
                        formatvalue = AgreedKey.GetFormatValue(eleNode.eleNodeList[0].SetValue, eleNode.eleNodeList[1].SetValue, ref errorinfo);
                        break;
                }
                return choice.PadLeft(2, '0') + formatvalue;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }


        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            try
            {
                int iresult = 0;
                string choice = frameData.Substring(0, 2);
                frameData = frameData.Substring(2);
                string Keyid = string.Empty;
                string keyParameters = string.Empty;
                string keyCipheredData = string.Empty;
                switch (int.Parse(choice))
                {
                    case 0:
                      Keyid = ((int)IdentifiedKey.AnalizeData(ref frameData, ref errorinfo)).ToString();
                      if (eleNode.eleNodeList.Count == 0)
                          eleNode.eleNodeList.Add(new ElementData());
                      eleNode.eleNodeList[0].ReadValue = Keyid;
                      break;
                    case 1:
                      iresult = WrappedKey.AnalizeData(ref frameData, ref Keyid, ref  keyCipheredData, ref errorinfo);
                      while (eleNode.eleNodeList.Count < 2)
                          eleNode.eleNodeList.Add(new ElementData());
                      eleNode.eleNodeList[0].ReadValue = Keyid;
                      eleNode.eleNodeList[1].ReadValue = keyCipheredData;
                      break;
                    case 2:
                      iresult = AgreedKey.AnalizeData(ref frameData, ref keyParameters, ref keyCipheredData, ref errorinfo);
                      while (eleNode.eleNodeList.Count < 2)
                          eleNode.eleNodeList.Add(new ElementData());
                      eleNode.eleNodeList[0].ReadValue = keyParameters;
                      eleNode.eleNodeList[1].ReadValue = keyCipheredData;
                      break;
                }
                eleNode.ReadValue = choice;
                
                return iresult;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }
    }

    
   
    /// <summary>
    /// 对象方法描述符OMD（Object Method Descriptor）,同OAD，直接继承
    /// </summary>
    public class CMD : CAD
    {        
    }

    /// <summary>
    /// TI用于表示时间间隔的间隔值及其时间单位；间隔值为0表示无间隔
    /// </summary>
    public class TI 
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            try
            {
                if (string.IsNullOrEmpty(eleNode.SetValue))
                {
                    return "00";
                }

                int iUnits = (int)(TIUnit)Enum.Parse(typeof(TIUnit), eleNode.Units);
                string Tivalue = longunsigned.GetFormatValue(eleNode,ref errorinfo);
                return iUnits.ToString().PadLeft(2, '0') + Tivalue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

        }
        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            if (string.IsNullOrEmpty(frameData))
            {
                return -1;
            }
            try
            {
                int iUnits = Convert.ToInt32(frameData.Substring(0, 2));
                frameData = frameData.Remove(0, 2);
                int iresult = longunsigned.AnalizeData(ref frameData, eleNode, ref errorinfo);

                eleNode.Units = Enum.GetName(typeof(TIUnit), (TIUnit)iUnits);
                eleNode.FrameValue = iUnits.ToString();

                return iresult;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }
    }

    public class TSA 
    {
        public static string GetFormatValue(ElementData eleNode)
        {
            try
            {
                string value = eleNode.SetValue;
                int DLen;
                if (eleNode.DataLen=="0" || string.IsNullOrEmpty(eleNode.DataLen))
                {
                    double dlen = Math.Ceiling((double)eleNode.SetValue.Length / 2);
                    DLen = (int)dlen;

                }
                else
                  DLen = Convert.ToInt32(eleNode.DataLen);
                value = value.Length >= DLen * 2 ? value.Substring(0, DLen * 2) : value.PadLeft(DLen * 2, '0');
                return MyConvert.DecToHex((DLen-1).ToString(), 2) + value; //地址的长度是实际长度减1

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }    

        /// <summary>
        /// 直接根据字符串和长度生成命令串
        /// </summary>
        /// <param name="eleValue"></param>
        /// <returns></returns>
        public static string GetFormatValue(string eleValue, string dlen)
        {
            try
            {
                int DLen = int.Parse(dlen);
                eleValue = eleValue.Length >= DLen * 2 ? eleValue.Substring(0, DLen * 2) : eleValue.PadLeft(DLen * 2, '0');

                return MyConvert.DecToHex((DLen-1).ToString(), 2)　+　eleValue;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }


        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            if (string.IsNullOrEmpty(frameData))
            {
                return -1;
            }
            try
            {
                int Dlen = Convert.ToInt32(frameData.Substring(1, 1), 16) + 1;　//地址长度为长度字节加1
                string value = frameData.Substring(2, Dlen * 2);
                frameData = frameData.Substring((Dlen + 1) * 2);//数据长度加1字节长度字节

                eleNode.ReadValue = value;
                eleNode.FrameValue = value;              
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;

            }
        }
    }
   
    //public class Region : DTBase
    //{
    //    public static string GetFormatValue(ElementData eleNode)
    //    {
    //        try
    //        {
    //            string FormatValue = string.Empty;
    //            int iUnits = (int)(RegionUnit)Enum.Parse(typeof(RegionUnit), eleNode.eleNodeList[0].SetValue);
    //            string strData1 = Data.GetFormatValue(eleNode.eleNodeList[1]);
    //            string strData2 = Data.GetFormatValue(eleNode.eleNodeList[2]);
    //            FormatValue = iUnits.ToString().PadLeft(2, '0') + strData1 + strData2;
    //            return FormatValue;
    //        }
    //        catch (Exception ex)
    //        {
    //            throw new Exception(ex.ToString());
    //        }
    //    }

    //    public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
    //    {
    //        if (string.IsNullOrEmpty(frameData))
    //        {
    //            return -1;
    //        }
    //        try
    //        {
    //            ElementData eleNodeUnit = eleNode.eleNodeList[0];
    //            int iUnits = Convert.ToInt32(frameData.Substring(0, 2));
    //            frameData = frameData.Remove(0, 2);
    //            eleNodeUnit.FrameValue = iUnits.ToString();
    //            eleNodeUnit.ReadValue = Enum.GetName(typeof(RegionUnit), (RegionUnit)iUnits);

    //            int iresult = Data.AnalizeData(ref frameData, eleNode.eleNodeList[1], ref errorinfo);
    //            if (iresult != 0)
    //            {
    //                return iresult;
    //            }

    //            iresult = Data.AnalizeData(ref frameData, eleNode.eleNodeList[2], ref errorinfo);
    //            if (iresult != 0)
    //            {
    //                return iresult;
    //            }

    //            return iresult;
    //        }
    //        catch (Exception ex)
    //        {
    //            errorinfo = ex.ToString();
    //            return -1;
    //        }

    //    }

    //}
    public class SUnit 
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            try
            {
                string FormatValue = string.Empty;
                
                int iUnits = (int)(UnitsEnum)Enum.Parse(typeof(UnitsEnum), eleNode.SetValue);
               
                FormatValue = MyConvert.DecToHex(iUnits.ToString(), 2);

                return FormatValue;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            if (string.IsNullOrEmpty(frameData))
            {
                return -1;
            }
            try
            {
                eleNode.ReadValue = Enum.GetName(typeof(UnitsEnum), (UnitsEnum)(int.Parse(MyConvert.HexToDec(frameData.Substring(0, 2)))));
                frameData = frameData.Substring(2);                
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }
        }
    }


    public class ScalerUnit 
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            try
            {
                string FormatValue = string.Empty;
                string strScaler = "";
                int iUnits = 0;
                if (eleNode.eleNodeList.Count == 0)
                {
                    string[] strlist = eleNode.SetValue.Split(',');
                    eleNode.SetValue = strlist[0];
                    strScaler = integer.GetFormatValue(eleNode,ref errorinfo );
                    iUnits = (int)(UnitsEnum)Enum.Parse(typeof(UnitsEnum), strlist[1]);                    
                }
                else {
                     strScaler = integer.GetFormatValue(eleNode.eleNodeList[0],ref errorinfo);
                     iUnits = (int)(UnitsEnum)Enum.Parse(typeof(UnitsEnum), eleNode.eleNodeList[1].SetValue);                    
                }
                FormatValue = strScaler + MyConvert.DecToHex(iUnits.ToString(), 2);              

                return FormatValue;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            if (string.IsNullOrEmpty(frameData))
            {
                return -1;
            }
            try
            {
                string strScaler = "";
                string sUnits = "";
                strScaler = eleNode.eleNodeList[0].ReadValue;
                sUnits = Enum.GetName(typeof(UnitsEnum), (UnitsEnum)(int.Parse(eleNode.eleNodeList[1].ReadValue)));
                eleNode.ReadValue = strScaler + ',' + sUnits;
                eleNode.eleNodeList.Clear();
                return 0;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return -1;
            }

        }




    }

    //public class MS : DTBase
    //{
    //    public static string GetFormatValue(ElementData eleNode)
    //    {
    //        try
    //        {
    //            string FormatValue = string.Empty;
    //            int iUnits = (int)(MSMode)Enum.Parse(typeof(MSMode), eleNode.SetValue);
    //            MSMode msMode = (MSMode)Enum.Parse(typeof(MSMode), eleNode.SetValue);
    //            StringBuilder strbFormatValue = new StringBuilder();
    //            switch (msMode)
    //            {
    //                case MSMode.Sel0:
    //                case MSMode.Sel1:
    //                    break;
    //                case MSMode.Sel2:
    //                    strbFormatValue.Append(MyConvert.DecToHex(eleNode.eleNodeList.Count.ToString(), 2));
    //                    foreach (ElementData childNode in eleNode.eleNodeList)
    //                    {
    //                        strbFormatValue.Append(unsigned.GetFormatValue(childNode));
    //                    }
    //                    break;
    //                case MSMode.Sel3:
    //                    strbFormatValue.Append(MyConvert.DecToHex(eleNode.eleNodeList.Count.ToString(), 2));
    //                    foreach (ElementData childNode in eleNode.eleNodeList)
    //                    {
    //                        strbFormatValue.Append(TSA.GetFormatValue(childNode));
    //                    }
    //                    break;
    //                case MSMode.Sel4:
    //                    strbFormatValue.Append(MyConvert.DecToHex(eleNode.eleNodeList.Count.ToString(), 2));
    //                    foreach (ElementData childNode in eleNode.eleNodeList)
    //                    {
    //                        strbFormatValue.Append(longunsigned.GetFormatValue(childNode));
    //                    }
    //                    break;
    //                case MSMode.Sel5:
    //                case MSMode.Sel6:
    //                case MSMode.Sel7:
    //                    strbFormatValue.Append(MyConvert.DecToHex(eleNode.eleNodeList.Count.ToString(), 2));
    //                    foreach (ElementData childNode in eleNode.eleNodeList)
    //                    {
    //                        strbFormatValue.Append(Region.GetFormatValue(childNode));
    //                    }
    //                    break;
    //            }

    //            FormatValue = iUnits.ToString().PadLeft(2, '0') + strbFormatValue.ToString();
    //            return FormatValue;
    //        }
    //        catch (Exception ex)
    //        {
    //            throw new Exception(ex.ToString());
    //        }
    //    }

    //    public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
    //    {
    //        if (string.IsNullOrEmpty(frameData))
    //        {
    //            return -1;
    //        }
    //        try
    //        {
    //            int iresult = 0;
    //            int Lens = 0;
    //            MSMode msMode = (MSMode)(int.Parse(MyConvert.HexToDec(frameData.Substring(0, 2))));
    //            frameData = frameData.Substring(2);

    //            switch (msMode)
    //            {
    //                case MSMode.Sel0:
    //                case MSMode.Sel1:
    //                    break;
    //                case MSMode.Sel2:
    //                    frameData = frameData.Substring(2);
    //                    Lens = eleNode.eleNodeList.Count;
    //                    for (int i = 0; i < Lens; i++)
    //                    {
    //                        iresult = unsigned.AnalizeData(ref frameData, eleNode.eleNodeList[i], ref errorinfo);
    //                        if (iresult != 0)
    //                        {
    //                            return iresult;
    //                        }
    //                    }
    //                    break;
    //                case MSMode.Sel3:
    //                    frameData = frameData.Substring(2);
    //                    Lens = eleNode.eleNodeList.Count;
    //                    for (int i = 0; i < Lens; i++)
    //                    {
    //                        iresult = TSA.AnalizeData(ref frameData, eleNode.eleNodeList[i], ref errorinfo);
    //                        if (iresult != 0)
    //                        {
    //                            return iresult;
    //                        }
    //                    }
    //                    break;
    //                case MSMode.Sel4:
    //                    frameData = frameData.Substring(2);
    //                    Lens = eleNode.eleNodeList.Count;
    //                    for (int i = 0; i < Lens; i++)
    //                    {
    //                        iresult = longunsigned.AnalizeData(ref frameData, eleNode.eleNodeList[i], ref errorinfo);
    //                        if (iresult != 0)
    //                        {
    //                            return iresult;
    //                        }
    //                    }
    //                    break;
    //                case MSMode.Sel5:
    //                case MSMode.Sel6:
    //                case MSMode.Sel7:
    //                    frameData = frameData.Substring(2);
    //                    Lens = eleNode.eleNodeList.Count;
    //                    for (int i = 0; i < Lens; i++)
    //                    {
    //                        iresult = Region.AnalizeData(ref frameData, eleNode.eleNodeList[i], ref errorinfo);
    //                        if (iresult != 0)
    //                        {
    //                            return iresult;
    //                        }
    //                    }
    //                    break;
    //            }
    //            return 0;
    //        }
    //        catch (Exception ex)
    //        {
    //            errorinfo = ex.ToString();
    //            return -1;
    //        }

    //    }

    //}

    public class SID 
    {       

        public static string GetFormatValue(ElementData eleNode)
        {
            try
            {
                string FormatValue = string.Empty;

                FormatValue = GetFormatValue(eleNode.SetValue);
                return FormatValue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }

        public static string GetFormatValue(string sIDData)
        {
            try
            {
                string[] SidList = sIDData.Split(',');
                string FormatValue = string.Empty;

                FormatValue = SidList[0].PadLeft(4 * 2, '0');

                string m_SIDData = SidList[1];
                m_SIDData = m_SIDData.Length % 2 == 0 ? m_SIDData : "0" + m_SIDData;
                FormatValue = FormatValue + MyConvert.DecToHex((m_SIDData.Length / 2).ToString(), 2) + m_SIDData;

                return FormatValue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
    public class SIDMAC
    {
        public static string GetFormatValue(ElementData eleNode)
        {
            try
            {
                string FormatValue = string.Empty;
                string[] SidList = eleNode.SetValue.Split(',');
                FormatValue = SID.GetFormatValue(eleNode);

                FormatValue = FormatValue + MyConvert.DecToHex((SidList[2].Length / 2).ToString(), 2) + SidList[2];

                return FormatValue;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
    public class COMDCB
    {
        public static string GetFormatValue(ElementData eleNode,ref string errorinfo)
        {
            try
            {
                string FormatValue = string.Empty;

                for (int i = 0; i < 5; i++)
                    FormatValue = FormatValue + ProEnum.GetFormatValue(eleNode.eleNodeList[i],ref errorinfo);

                //int intChoice = (int)(BaudRate)Enum.Parse(typeof(BaudRate),eleNode.eleNodeList[0].SetValue);
                //FormatValue =MyConvert.DecToHex(intChoice.ToString(),2);

                //intChoice = (int)(ParityBit)Enum.Parse(typeof(ParityBit), eleNode.eleNodeList[1].SetValue);
                //FormatValue = FormatValue + MyConvert.DecToHex(intChoice.ToString(), 2);

                //intChoice = (int)(DataBit)Enum.Parse(typeof(DataBit), eleNode.eleNodeList[2].SetValue);
                //FormatValue = FormatValue + MyConvert.DecToHex(intChoice.ToString(), 2);

                //intChoice = (int)(StopBit)Enum.Parse(typeof(StopBit), eleNode.eleNodeList[3].SetValue);
                //FormatValue = FormatValue + MyConvert.DecToHex(intChoice.ToString(), 2);

                //intChoice = (int)(FlowControl)Enum.Parse(typeof(FlowControl), eleNode.eleNodeList[4].SetValue);
                //FormatValue = FormatValue + MyConvert.DecToHex(intChoice.ToString(), 2);

                return FormatValue;
            }
            catch (Exception ex)
            {
                errorinfo = ex.ToString();
                return string.Empty;
            }
        }

        public static int AnalizeData(ref string frameData, ElementData eleNode, ref string errorinfo)
        {
            for (int i = 0; i < 5; i++)
            {
                ProEnum.AnalizeData(ref frameData, eleNode.eleNodeList[i], ref errorinfo);
                eleNode.ReadValue = eleNode.ReadValue + "," + eleNode.eleNodeList[i].ReadValue;
            }
            eleNode.ReadValue = eleNode.ReadValue.Substring(1);

            //string values = frameData.Substring(0, 2);
            //eleNode.eleNodeList[0].FrameValue=values;
            //eleNode.eleNodeList[0].ReadValue = Enum.GetName(typeof(BaudRate), (BaudRate)int.Parse(MyConvert.HexToDec(values)));
            //frameData.Substring(2);

            //values = frameData.Substring(0, 2);
            //eleNode.eleNodeList[1].FrameValue = values;
            //eleNode.eleNodeList[1].ReadValue = Enum.GetName(typeof(ParityBit), (ParityBit)int.Parse(MyConvert.HexToDec(values)));
            //frameData.Substring(2);

            //values = frameData.Substring(0, 2);
            //eleNode.eleNodeList[2].FrameValue = values;
            //eleNode.eleNodeList[2].ReadValue = Enum.GetName(typeof(DataBit), (DataBit)int.Parse(MyConvert.HexToDec(values)));
            //frameData.Substring(2);

            //values = frameData.Substring(0, 2);
            //eleNode.eleNodeList[3].FrameValue = values;
            //eleNode.eleNodeList[3].ReadValue = Enum.GetName(typeof(StopBit), (StopBit)int.Parse(MyConvert.HexToDec(values)));
            //frameData.Substring(2);

            //values = frameData.Substring(0, 2);
            //eleNode.eleNodeList[4].FrameValue = values;
            //eleNode.eleNodeList[4].ReadValue = Enum.GetName(typeof(FlowControl), (FlowControl)int.Parse(MyConvert.HexToDec(values)));
            //frameData.Substring(2);

            return 0;
        }
    } 


}
